﻿using System.Collections.Generic;
using Beisen.Data;
using fKanban.Model;
using fKanban.Utility.ObjectExtend;

namespace fKanban.Provider
{
    public class SprintGoalProvider
    {
        public int Add(SprintGoalEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            var identity = SafeProcedure.ExecuteScalar(db, "dbo.jGoalAdd", delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@DashboardId", e.DashboardId);
                parameters.AddWithValue("@UserStoryId", e.UserStoryId);
                parameters.AddWithValue("@SprintId", e.SprintId);
                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Description", e.Description);
            });
            return identity.ToInt();
        }
        public int Update(SprintGoalEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jGoalUpdateByPara",
            delegate(IParameterSet parameters)
            {
                /*
                    @ID INT = 0,
                    @DashboardID INT = 0,
                    @UserStoryID INT = 0,
                    @SprintID INT = 0,
                    @Name NVARCHAR(20) = NULL,
                    @Description NVARCHAR(400) = NULL
                 */
                parameters.AddWithValue("@Id", e.Id);
                parameters.AddWithValue("@DashboardID", e.DashboardId);
                parameters.AddWithValue("@UserStoryID", e.UserStoryId);
                parameters.AddWithValue("@SprintID", e.SprintId);
                parameters.AddWithValue("@Name", e.Name);
                parameters.AddWithValue("@Description", e.Description);
            });
        }
        public int Remove(SprintGoalEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteNonQuery(db, "dbo.jGoalRemoveByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@ID", e.Id);
            });
        }
        public List<SprintGoalEntity> LoadData(SprintGoalEntity e)
        {
            Database db = Database.GetDatabase("fkanban");
            return SafeProcedure.ExecuteAndGetInstanceList<SprintGoalEntity>(db, "dbo.jGoalGetListByPara",
            delegate(IParameterSet parameters)
            {
                parameters.AddWithValue("@SprintID", e.SprintId);
            },
            delegate(IRecord record, SprintGoalEntity objectInstance)
            {
                objectInstance.Id = record.GetOrDefault<int>("Id", 0);
                objectInstance.UserStoryId = record.GetOrDefault<int>("UserStoryId", 0);
                objectInstance.SprintId = record.GetOrDefault<int>("SprintId", 0);
                objectInstance.DashboardId = record.GetOrDefault<int>("DashboardId", 0);

                objectInstance.Name = record.GetOrDefault<string>("Name", string.Empty);
                objectInstance.Description = record.GetOrDefault<string>("Description", string.Empty);
            });
        }
    }
}